package com.jsyso.weixin.api;

import java.io.File;

import org.apache.commons.lang3.StringUtils;

import com.jsyso.jsyso.lang.JStringUtils;
import com.jsyso.jsyso.util.JsMap;

/**
 * 素材管理
 */
public class MediaAPI extends API {
	public static enum MediaType {
        IMAGE, VOICE, VIDEO, THUMB, NEWS;
        public String get() {
            return this.name().toLowerCase();
        }
    }
	
	// 新增临时素材
    protected static String MEDIA_UPLOAD = "/cgi-bin/media/upload?access_token=";
	
    public API uploadMedia(String appid, MediaType mediaType, File file) {
    	// 获取token
		JsMap resMap = AccessTokenAPI.getAccessToken(appid);
		String accessToken = resMap.get("access_token", String.class);
		if(StringUtils.isNotBlank(accessToken)) {
			this.method = RequestMethod.UPLOAD;
	    	this.file = file;
	    	this.url = JStringUtils.newBuilder(API_WEIXIN_QQ_COM, MEDIA_UPLOAD, accessToken)
	    			.append("&type=").append(mediaType.get()).toString();
		}
    	return this;
    }
}
